<?php  if (! defined('BASEPATH')) exit('No direct script access allowed');
class home_model extends CI_Model
{
	protected $lang ;
	function __construct() 
	{
		parent::__construct();
	}
	
	public function initialise($lang)
    {
		$this->lang = $lang;
    }
	
	public function hotnews()
	{
		$this->db->where('bl_active', 1);
		$this->db->where('is_home', 1);
		$this->db->where('is_hot', 1);
		$this->db->order_by('dt_create','asc');
		$query = $this->db->get('news', 12, 0) ;
		return $query->result() ;
	}
//******************** Category ***************		
	/*public function category($catid=0)
	{
		$this->db->select('news.*, news_category.name_block');
		if($catid) $this->db->where('news.cat_id', $catid);
		$this->db->where('news.bl_active', 1);
		$this->db->where('is_home', 1);
		$this->db->order_by('dt_create','desc');
		$this->db->join('news_category','news_category.cat_id=news.cat_id');
		$query = $this->db->get('news', 3, 0) ;
		return $query->result() ;
	}*/
	
	//get list hot category
	public function category($catid=0,$num=7,$offset=0)
	{
		$arr_cat = $this->get_sub_category($catid) ; //trit($arr_cat);
		
		$this->db->select('news.*,news_category.cat_name, name_block');
		$this->db->where_in('news.cat_id', $arr_cat);
		$this->db->where('news.bl_active', 1);
		$this->db->where('is_home', 1);
		$this->db->where('news.is_hot', 1);
		$this->db->order_by('news.dt_create','desc');
		$this->db->join('news_category','news_category.cat_id=news.cat_id');
		$query = $this->db->get('news', $num,$offset) ;
		return $query->result() ;
	}
	//get_sub category
	private function get_sub_category($catid=0)
	{
		$this->db->select('news_category.cat_id');
		$this->db->where('parent_id', $catid);
		$this->db->where('bl_active', 1);
		$this->db->order_by('nb_order','desc');
		$query = $this->db->get('news_category') ;
		$result = $query->result() ;
		
		$arr = array($catid) ;
		foreach ($result as $rs) :
			array_push($arr, $rs->cat_id) ;
		endforeach ;
		return $arr ;
	}
	
	public function get_cat_name($catid=0)
	{
		$this->db->where('cat_id', $catid);
		$this->db->where('bl_active', 1);
		$query = $this->db->get('product') ;
		return $query->row() ;
	}
	
	function getProduct($table,$num,$offset,$pro_id)
	{
		$this->db->select('pro_id, name_'.$this->lang.' as name, content_'.$this->lang.' as content,lb_image');
		$this->db->where('pro_id >',$pro_id);
		$this->db->where('bl_active',1);
		$this->db->order_by('nb_order','ASC');
		$result=$this->db->get($table,$num,$offset)->result();
		return $result;
	}
	
	function getProFromCat($table,$cat_id,$num,$offset)
	{
		$this->db->select('pro_id, name_'.$this->lang.' as name, content_'.$this->lang.' as content,lb_image');
		$this->db->where('catid',$cat_id);
		$this->db->where('bl_active',1);
		$this->db->order_by('nb_order','ASC');
		$result=$this->db->get($table,$num,$offset)->result();
		return $result;
	}

}
?>